package cn.yr.clock.controller;

import cn.yr.clock.domain.entity.Engine;
import cn.yr.clock.domain.entity.Prize;
import cn.yr.clock.domain.entity.PrizeInstance;
import cn.yr.clock.domain.vo.PageVo;
import cn.yr.clock.domain.vo.PrizeVo;
import cn.yr.clock.handler.CacheUserHandler;
import cn.yr.clock.service.PrizeInstanceService;
import cn.yr.clock.service.PrizeService;
import cn.yr.common.exception.ret.Ret;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;


/**
 * 
 * @author Lang
 * @email 1102076808@qq.com
 * @date 2021-09-25 11:13:41
 */
@RestController
@RequestMapping("/prize")
public class PrizeController{
    @Autowired
    private PrizeService prizeService;
    @Autowired
    private PrizeInstanceService prizeInstanceService;
    @Autowired
    private CacheUserHandler cacheUserHandler;
    /**
     * 领奖
     * @param engine
     * @return
     */
    @RequestMapping("/receivePrice")
    public Ret<PrizeInstance>  receivePrice(@RequestBody Engine engine){
        Long userId = cacheUserHandler.getUser().getUserId();
        return prizeService.receivePrice(engine,userId);
    }

    /**
     * 列表
     */
    @RequestMapping("/page")
    public Ret<PageVo<Prize>> page(@RequestBody PageVo<Prize> pageVo){
        QueryWrapper<Prize> wrapper = new QueryWrapper<>();
        PageVo<Prize> pagePrize = prizeService.page(pageVo,wrapper);
        return Ret.success(pagePrize);
    }

    @RequestMapping("/list")
    public Ret<List<Prize>> list(){
        List<Prize> list = prizeService.list();
        return Ret.success(list);
    }

    @RequestMapping("/listPrizeInventory")
    public Ret<List<PrizeVo>> listPrizeInventory(){
        List<PrizeVo> list = prizeService.selectPrizeInventory();
        return Ret.success(list);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{prizeId}")
    public Ret<Prize> info(@PathVariable("prizeId") Long prizeId){
		Prize  prize = prizeService.getById(prizeId);
	    return Ret.success(prize);
    }

    /**
     * 保存
     */
    @RequestMapping("/add")
    public Ret<Object> save(@Validated @RequestBody Prize prize){
		prizeService.save(prize);
	    return Ret.success();
    }

    /**
     * 修改
     */
    @RequestMapping("/edit")
    public Ret<Object> update(@Validated @RequestBody Prize prize){
		prizeService.updateById(prize);
	    return Ret.success();
    }

    /**
     * 删除
     */
    @RequestMapping("/remove")
    public Ret<Object> delete(@RequestBody Long[] prizeIds){
		prizeService.removeByIds(Arrays.asList(prizeIds));
        return Ret.success();
    }

}
